package com.jingdianjichi.user.controller;


import cn.hutool.system.SystemUtil;
import com.jingdianjichi.common.Result;
import com.jingdianjichi.redis.utils.CacheUtil;
import com.jingdianjichi.redis.utils.RedisShareLockUtil;
import com.jingdianjichi.redis.utils.RedisUtil;
import com.jingdianjichi.tool.ExportWordUtil;
import com.jingdianjichi.user.entity.dto.UserDto;
import com.jingdianjichi.user.entity.req.SysUserReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisShareLockUtil redisShareLockUtil;

    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping("/testMVC")
    public String testMVC() {
        return "mvc test success";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisUtil.set("name", "logos");
    }

    @GetMapping("/testRedisLock")
    public Result testRedisLock() {
        boolean result = redisShareLockUtil.lock("logos", "123321", 100000L);
        log.info("result:{}", result);
        return Result.ok(result);
    }

    @GetMapping("/testLog")
    public Result testLog() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            log.info("这是第{}条日志", i);
        }
        // 异步 69 52 83
        // 同步 1184 1303 1138
        long endTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        sb.append("总耗时：").append(endTime-startTime).append("ms");
        log.info(sb.toString());
        return Result.ok(sb.toString());
    }

    @PostMapping("/testAop")
    public Result testAop(@RequestBody UserDto userDto) {
        return Result.ok(true);
    }


    @GetMapping("/testExport")
    public void testExport() throws Exception {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "logos");
        dataMap.put("auditName", "板烧鸡腿堡");
        ExportWordUtil.exportWord(dataMap, "导出文件", "wordExport.ftl");
    }

    @GetMapping("/testLocalCache")
    public void testLocalCache(List<Long> skuIdList) {
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfo = getSkuInfo(skuIdList);
            return skuInfo;
        });
        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuPrice.class, (list) -> {
            Map<Long, SkuPrice> skuPrice = getSkuPrice(skuIdList);
            return skuPrice;
        });
    }

    // 模拟RPC接口(可以使用OpenFeign来作为Service层的RPC调用)
    public Map<Long, SkuInfo> getSkuInfo(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    public Map<Long, SkuPrice> getSkuPrice(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    class SkuInfo {
        private Long id;
        private String name;
    }

    class SkuPrice {
        private Long id;
        private Double price;
    }


    @PostMapping("/testQuery")
    public void testQuery(@RequestBody SysUserReq sysUserReq) {
        // 2024-12-08 22:02:25
        System.out.println(sysUserReq);
    }

}
